//
//  PageTitleView.swift
//  SwiftDemo
//
//  Created by liguoqing on 2022/6/19.
//  Copyright © 2022 guoqing.li. All rights reserved.
//

import UIKit

// MARK:  定义常量
private let kScrollLineW = 20.0
private let kScrollLineH = 2.0
private let kNormalColor:(CGFloat,CGFloat,CGFloat) = (85,85,85)
private let kSelectColor:(CGFloat,CGFloat,CGFloat) = (255,128,0)

// MARK: 定义协议
protocol PageTitleViewDelegate:AnyObject{
    func pageTitleView(titleView:PageTitleView, selectIndex:Int)
}

class PageTitleView: UIView
{
    // MARK: 定义属性
    private var currentIndex: Int = 0
    private let titles: [String]
    weak var delegate:PageTitleViewDelegate?
    // MARK: 懒加载
    private lazy var scrollView: UIScrollView = {
        let scrollView = UIScrollView.init();
        scrollView.showsHorizontalScrollIndicator = false;
        scrollView.scrollsToTop = false
        scrollView.bounces = false
        return scrollView
    }()
    
    private lazy var titleLabels: [UILabel] = [UILabel]()
    
    private lazy var scrollLine: UIView = {
        let scrollLine = UIView();
        scrollLine.backgroundColor = .orange
        return scrollLine
    }()
    
    // MARK: 自定义构造方法
    init(frame:CGRect, titles:[String]){
        self.titles = titles
        super.init(frame: frame);
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension PageTitleView
{
    private func setupUI(){
        // 1. 添加scrollView
        setupScrollView()
        // 2.添加label
        setupTitleLabels()
        // 3.设置底线和滑块
        setupBottomLine()
        
    }
    private func setupScrollView(){
        addSubview(scrollView)
        scrollView.snp.makeConstraints { make in
            make.edges.equalTo(0)
        }
    }
    
    private func setupTitleLabels(){
        let labelW : CGFloat = bounds.width/CGFloat(titles.count)
        let labelH : CGFloat = bounds.height
        let labelY : CGFloat = 0
        for (index, title) in titles.enumerated() {
            let label = UILabel.init()
            label.text = title;
            label.font = .systemFont(ofSize: 16.0)
            label.tag = index
            label.textColor = UIColor(r: kNormalColor.0, g: kNormalColor.1, b: kNormalColor.2)
            label.textAlignment = .center
            let labelX : CGFloat = labelW * CGFloat(index)
            label.frame = CGRect(x: labelX, y: labelY, width: labelW, height: labelH)
            // 添加label 在scorllView 上
            self.scrollView.addSubview(label)
            // 保存label
            self.titleLabels.append(label)
            // 添加手势
            label.isUserInteractionEnabled = true
            let gesture = UITapGestureRecognizer(target: self, action: #selector(self.titleLabelClick(gapG:)))
            label.addGestureRecognizer(gesture)
        }
    }
    private func setupBottomLine() {
        // 添加下划线
        let bottomLine = UIView()
        bottomLine.backgroundColor = .lightGray
        addSubview(bottomLine)
        bottomLine.snp.makeConstraints { make in
            make.leading.bottom.trailing.equalTo(0)
            make.height.equalTo(0.5)
        }
        // 获取当前label，并修改为选中颜色
        guard let firstLabel = titleLabels.first else { return }
        firstLabel.textColor = UIColor(r: kSelectColor.0, g: kSelectColor.1, b: kSelectColor.2)
        
        // 添加scorllViewLine
        scrollView.addSubview(scrollLine)
        scrollLine.snp.makeConstraints { make in
            make.centerX.equalTo(firstLabel.snp.centerX)
            make.bottom.equalTo(firstLabel.snp.bottom)
            make.width.equalTo(firstLabel.snp.width)
            make.height.equalTo(kScrollLineH)
        }
    }
}

extension PageTitleView
{
    @objc private func titleLabelClick(gapG:UIGestureRecognizer){
        // 0. 获取当前label
        guard let currentLabel = gapG.view as? UILabel else { return }
        // 1. 判断label的索引是否时当前的索引
        if currentLabel.tag == currentIndex {return}
        // 2. 更新label的字体颜色
        let oldLabel = self.titleLabels[currentIndex];
        
        currentLabel.textColor = UIColor(r: kSelectColor.0, g: kSelectColor.1 , b: kSelectColor.2)
        oldLabel.textColor = UIColor(r: kNormalColor.0, g: kNormalColor.1, b: kNormalColor.2)
        // 3. 更新当前索引
        currentIndex = currentLabel.tag
        // 4. 更新底部下划线位置
        UIView.animate(withDuration: 0.15) { [self] in
            self.scrollLine.snp.remakeConstraints(){ make in
                make.centerX.equalTo(currentLabel.snp.centerX)
                make.bottom.equalTo(currentLabel.snp.bottom)
                make.width.equalTo(currentLabel.snp.width)
                make.height.equalTo(kScrollLineH)
            }
        }
        // 5.代理回调
        delegate?.pageTitleView(titleView: self, selectIndex: currentIndex)
    }
}

// MARK: 对外暴露的方法
extension PageTitleView {
    
    func setTitleWithProgress(progress: CGFloat, sourceIndex: Int, targetIndex: Int) {
        
        //1. 取出 sourceLabel/targetLabel
        let sourceLabel = titleLabels[sourceIndex]
        let targetLabel = titleLabels[targetIndex]
        
        //2. 处理滑块逻辑
        let moveTotalX = targetLabel.frame.origin.x - sourceLabel.frame.origin.x
        let moveX = moveTotalX * progress
        scrollLine.frame.origin.x = sourceLabel.frame.origin.x + moveX
        
        //3. 颜色渐变(复杂)
        //3.1 取出变化的范围
        let colorDelta = (kSelectColor.0 - kNormalColor.0, kSelectColor.1 - kNormalColor.1, kSelectColor.2 - kNormalColor.2)
        
        //3.2 变化 sourceLabel
        sourceLabel.textColor = UIColor(r: kSelectColor.0 - colorDelta.0 * progress, g: kSelectColor.1 - colorDelta.1 * progress, b: kSelectColor.2 - colorDelta.2 * progress)
        
        //3.3 变化 targetLabel
        targetLabel.textColor = UIColor(r: kNormalColor.0 + colorDelta.0 * progress, g: kNormalColor.1 + colorDelta.1 * progress, b: kNormalColor.2 + colorDelta.2 * progress)
        
        //4. 记录最新的 index
        currentIndex = targetIndex
        
    }
    
}
                          

